package com.league.zjxn.main.mine.adapter

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ConvertUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.league.zjxn.R
import com.league.zjxn.entity.*
import com.league.zjxn.main.mine.ApproveDetailsActivity
import com.module.lemlin.image.loadRoundImage

class FollowEnterpriseAdapter :
    BaseQuickAdapter<FollowEnterprise, BaseViewHolder>(R.layout.item_follow_list) {
    override fun convert(holder: BaseViewHolder, item: FollowEnterprise) {
        holder.setText(R.id.tvFollowListTitle, item.title)
            .getView<ImageView>(R.id.ivFollowListIcon)
            .loadRoundImage(item.logo, radius = ConvertUtils.dp2px(10f))
    }
}

class FollowProjectAdapter :
    BaseQuickAdapter<FollowProject, BaseViewHolder>(R.layout.item_follow_list) {
    override fun convert(holder: BaseViewHolder, item: FollowProject) {
        holder.setText(R.id.tvFollowListTitle, item.title)
            .getView<ImageView>(R.id.ivFollowListIcon)
            .loadRoundImage(item.cover, radius = ConvertUtils.dp2px(10f))
    }
}

class ProjectApplyAdapter :
    BaseQuickAdapter<ProjectApply, BaseViewHolder>(R.layout.item_project_apply) {
    override fun convert(holder: BaseViewHolder, item: ProjectApply) {
        holder.setText(R.id.tvProjectApplyCycle, item.type)
            .setText(R.id.tvProjectApplyTitle, item.title)
//            .setText(R.id.tvProjectApplyPeople, "申请人：${item.enterprise}")
            .setText(R.id.tvProjectApplyTime, "申请时间：${item.create_time}")
        holder.getView<View>(R.id.vwProjectApplyPoint).apply {
            val res = when {
                item.type.contains("一周") -> {
                    R.drawable.bg_delivery_10dp_apply_1
                }
                item.type.contains("一个月") -> {
                    R.drawable.bg_delivery_10dp_apply_2
                }
                else -> {
                    R.drawable.bg_delivery_10dp_apply_3
                }
            }
            setBackgroundResource(res)
        }
        holder.getView<View>(R.id.vwProjectApplyShort).apply {
            visibility = if (holder.layoutPosition == 0) {
                View.INVISIBLE
            } else {
                View.VISIBLE
            }
        }
        holder.getView<View>(R.id.vwProjectApplyLong).apply {
            visibility = if (holder.layoutPosition == data.size - 1) {
                View.INVISIBLE
            } else {
                View.VISIBLE
            }
        }
    }
}

class EnterpriseApproveAdapter :
    BaseQuickAdapter<EnterpriseApprove, BaseViewHolder>(R.layout.item_approve_list) {
    init {
        setOnItemClickListener { _, view, i ->
            val data = getItem(i)
            ApproveDetailsActivity.open(data)
        }
    }

    override fun convert(holder: BaseViewHolder, item: EnterpriseApprove) {
        holder.setText(R.id.tvApproveListName, item.name)
            .setText(R.id.tvApproveListStatusText, item.status_text)
            .setText(R.id.tvApproveListContent, item.content)
            .setText(R.id.tvApproveListArea, "${item.province}\t${item.city}")
        holder.getView<TextView>(R.id.tvApproveListStatusText).apply {
            val color = when (item.status) {
                5 -> R.color.FF_17B572
                6 -> R.color.FF_FF3527
                else -> R.color.FF_FF8127
            }
            setTextColor(ColorUtils.getColor(color))
        }
        holder.getView<View>(R.id.view1).apply {
            val color = when (item.status) {
                5 -> R.drawable.bg_radius_10dp_approve_2
                6 -> R.drawable.bg_radius_10dp_approve_3
                else -> R.drawable.bg_radius_10dp_approve_1
            }
            setBackgroundResource(color)
        }
    }
}

class ApproveFlowAdapter :
    BaseQuickAdapter<ApproveFlow, BaseViewHolder>(R.layout.item_approve_flow) {
    override fun convert(holder: BaseViewHolder, item: ApproveFlow) {
        holder.setText(R.id.tvApproveFlowName, item.text)
            .setText(R.id.tvApproveFlowTime, "申请时间：${item.create_time}")
//        holder.getView<View>(R.id.vwProjectApplyPoint).apply {
//            val res = when {
//                item.type.contains("一周") -> {
//                    R.drawable.bg_delivery_10dp_apply_1
//                }
//                item.type.contains("一个月") -> {
//                    R.drawable.bg_delivery_10dp_apply_2
//                }
//                else -> {
//                    R.drawable.bg_delivery_10dp_apply_3
//                }
//            }
//            setBackgroundResource(res)
//        }
        holder.getView<View>(R.id.vwApproveFlowShort).apply {
            visibility = if (holder.layoutPosition == 0) {
                View.INVISIBLE
            } else {
                View.VISIBLE
            }
        }
        holder.getView<View>(R.id.vwApproveFlowLong).apply {
            visibility = if (holder.layoutPosition == data.size - 1) {
                View.INVISIBLE
            } else {
                View.VISIBLE
            }
        }
        holder.getView<View>(R.id.vwApproveFlowPoint).apply {
            val res = if (item.status == "1" || holder.layoutPosition == 0)
                R.drawable.bg_delivery_10dp_apply_1 else R.drawable.bg_radius_10dp_apply
            setBackgroundResource(res)
        }

        holder.getView<TextView>(R.id.tvApproveFlowStatus).apply {
            visibility = when {
                item.status.isNullOrEmpty() -> View.INVISIBLE
                item.status == "0" -> View.INVISIBLE
                else -> View.VISIBLE
            }
            val color = when (item.status) {
                "1" -> R.color.FF_17B572
                "2" -> R.color.FF_FF3527
                else -> R.color.FF_FF8127
            }
            setTextColor(ColorUtils.getColor(color))
            val textStr = when (item.status) {
                "1" -> "已批准"
                "2" -> "驳回"
                else -> "待审核"
            }
            text = textStr
        }

        holder.getView<TextView>(R.id.tvApproveFlowTime).apply {
            visibility = if (item.create_time.isNullOrEmpty()) View.INVISIBLE else View.VISIBLE
        }

        holder.getView<TextView>(R.id.tvApproveFlowMarks).apply {
            visibility = if (item.status == "1" || holder.layoutPosition == 0)
                View.VISIBLE else View.INVISIBLE
            text = if (item.status == "1" && holder.layoutPosition == (data.size - 1)) {
                "已同意"
            } else {
                "拟同意，呈${holder.layoutPosition + 1}级管理员阅"
            }
        }
    }
}